﻿namespace EFS.RC.CommunicatorContext.Common.WS
{
	public static class Setup
	{
		/// Get security mode
		private static System.ServiceModel.BasicHttpSecurityMode GetBasicHttpSecurityMode(
			System.String absoluteUrl)
		{
			// init value
			System.ServiceModel.BasicHttpSecurityMode retValue = System.ServiceModel.BasicHttpSecurityMode.None;

			// prepare url
			System.String urlTemp = EFS.Common.Functions.String.GetLowerTrimmedString(absoluteUrl);
			if (EFS.Common.Verify.String(urlTemp))
			{
				if (urlTemp.StartsWith("https:"))
				{
					retValue = System.ServiceModel.BasicHttpSecurityMode.Transport;
				}
			}

			// ok
			return retValue;
		}

		/// Property UtilitiesService: Get, Set
		public static ServiceReference.ProxySoapClient NewUtilitiesService
		{
			get
			{
				System.String url = System.Windows.Browser.HtmlPage.Document.DocumentUri.ToString();
				url = url.Substring(0, url.LastIndexOf("/")) + "/EFS.RC.CommunicatorContext.WebProxy/Proxy.asmx";

				// set bindings
				System.ServiceModel.BasicHttpBinding bind = new System.ServiceModel.BasicHttpBinding();
				bind.Security.Mode = GetBasicHttpSecurityMode(url);
				bind.MaxBufferSize = 2147483647;
				bind.MaxReceivedMessageSize = 2147483647;
				System.ServiceModel.EndpointAddress endpoint = new System.ServiceModel.EndpointAddress(url);

				// create service
				ServiceReference.ProxySoapClient utilitiesService = new ServiceReference.ProxySoapClient(bind, endpoint);

				// ok
				return utilitiesService;
			}
		}
	}

}